#include <iostream>

#if defined __unix__

#include <jsonplus/cjson.h>
#include <jsonplus/cjsonparser.h>

#elif defined _WIN32

#include <cjson.h>

#endif

#if defined CJSON_USE_UNICODE
#define jcout wcout
#else
#define jcout cout
#endif

using namespace std;

int main(int argc, char *argv[])
{
  try
  {
    CJsonObject json_object;

    json_object.AddPare("name0", (jint)(-100));
		
  	jstring tmp = json_object.ToString();

		jcout << "json_object: " << tmp << endl;

    CJsonObject *obj = CJsonParser::Execute(tmp);
  	jcout << "json_object restructed: " << obj->ToString() << endl;
		delete obj;

		jstring str("{\"cmd\":\"get_chats\",\"session\":{\"user_id\":53,\"temp_token\":\"60f4add1-8ee2-4524-95b0-962917c50f2a\"},\"location\":false,\"x\":12.30,\"y\":55.6}");
		jcout << "original: " << str << endl;
    obj = CJsonParser::Execute(str);
  	jcout << "json_object restructed: " << obj->ToString() << endl;
  

		return 0;
		#if 0

    json_object.AddPare("name0", (jint)12);
    json_object.AddPare("name1", &json_value_num);
    json_object.AddPare("name2", "value");
    json_object.AddPare("name3", &json_value_string);
    json_object.AddPare("name4", true);
    json_object.AddPare("name5", &json_value_bool);
    json_object.AddPare("name6", (const jchar*)NULL);
    json_object.AddPare("name7", &json_value_null);
    json_object.AddPare("name8", &json_value_float);
    json_object.AddPare("name9", 15.5);

    jcout << "json_object: " << json_object.ToString() << endl;

    CJsonArray json_array;

    json_array.AddMember(&json_value_num);
    json_array.AddMember(&json_value_string);
    json_array.AddMember(&json_value_bool);
    json_array.AddMember(&json_value_null);
    json_array.AddMember(&json_value_float);
    json_array.AddMember((jint)12);
    json_array.AddMember("value");
    json_array.AddMember(true);
    json_array.AddMember((const jchar *)NULL);
    json_array.AddMember(15.5);

    jcout << "json_array: " << json_array.ToString() << endl;

    json_object.AddPare("name10", (CJsonValue*)&json_array);
    jcout << "json_object: " << json_object.ToString() << endl;

    CJsonArray json_array0;

    json_array0.AddMember(&json_value_num);
    json_array0.AddMember(&json_value_string);
    json_array0.AddMember(&json_value_bool);
    json_array0.AddMember(&json_value_null);
    json_array0.AddMember(&json_value_float);
    json_array0.AddMember((jint)12);
    json_array0.AddMember("value");
    json_array0.AddMember(true);
    json_array0.AddMember((const jchar *)NULL);
    json_array0.AddMember(15.5);
    json_array0.AddMember(&json_object);
    json_array0.AddMember(&json_array);

    jcout << "json_array0: " << json_array0.ToString() << endl;

    json_object.AddPare("name11", &json_array0);
    jcout << "json_object: " << json_object.ToString() << endl;

    jstring tmp = json_object.ToString();
    CJsonObject *obj = CJsonParser::Execute(tmp);
    
    jcout << "json_object restructed: " << obj->ToString() << endl;

    // Check if there is such a parameter in json object
    
    if ((*obj)["name0"])
    {
      jcout << "name0: " << (*obj)["name0"]->ToString() << endl;
    }

    if ((*obj)["name-1"])
    {
      jcout << "name-1" << (*obj)["name-1"]->ToString() << endl;
    }

    // Iterate in json object
    {
    vector <jstring> names;
    obj->GetNames(names);

    vector<jstring>::iterator it;
    
    for (it = names.begin(); it != names.end(); it++)
    {
      jcout << (*it) << ": " << ((*obj)[(*it)] ? (*obj)[(*it)]->ToString() : "doesn't exist") << endl;
    }
    }
    // Two ways to iterate in json array
    {
    for (unsigned i = 0; i < json_array0.Size(); i++)
    {
      jcout << i << ": " << json_array0[i]->ToString() << endl;
    }

    vector <CJsonValue*> array;
    vector<CJsonValue*>::iterator it;

    json_array0.GetValue(array);

    for (it = array.begin(); it != array.end(); it++)
    {
      jcout << (*it)->ToString() << endl;
      delete (*it);
    }
    }
		#endif
    delete obj;
  }
  catch (CJsonError & e)
  {
    cout << "Error: " << e.GetMessage() << endl;
  }
   
  return 0;
}

